package com.yoso.shop.interfaces.controller.order;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.yoso.api.info.log.OrderLogInfo;
import com.yoso.base.infrastructure.constants.ErrorMsg;
import com.yoso.base.interfaces.controller.BaseController;
import com.yoso.base.interfaces.controller.BaseResponse;
import com.yoso.base.interfaces.vo.PageVo;
import com.yoso.base.service.IBaseService;
import com.yoso.shop.application.fegin.LogFeignServer;
import com.yoso.shop.application.service.IOrderService;
import com.yoso.shop.domain.order.OrderEntity;
import com.yoso.shop.domain.order.OrderEntityExample;
import com.yoso.shop.infrastructure.factory.ShopBeanFactory;
import com.yoso.shop.interfaces.vo.order.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shizejin
 * @date 2022/1/28
 * @description:
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单信息")
@Slf4j
@CrossOrigin
public class OrderController extends BaseController<OrderVo, OrderEntity> {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ShopBeanFactory shopBeanFactory;
    @Autowired
    private LogFeignServer logFeignServer;

    @Override
    public OrderEntity V2E(OrderVo data) {
        return JSON.parseObject(JSON.toJSONString(data), OrderEntity.class);
    }

    @Override
    public OrderVo E2V(OrderEntity data) {
        return JSON.parseObject(JSON.toJSONString(data), OrderVo.class);

    }

    @Override
    public IBaseService<OrderVo, OrderEntityExample> getService() {
        return this.orderService;
    }

    @ApiOperation("增加订单")
    @PostMapping("/addOrder")
    public BaseResponse addOrder(@RequestBody List<String> cartIdList) {
        if (CollUtil.isEmpty(cartIdList)) {
            return this.error("购物项" + ErrorMsg.MUST_NOT_NULL);
        }
        return orderService.addOrder(cartIdList);
    }

    @ApiOperation("取消订单")
    @PostMapping("/cancelOrder")
    public BaseResponse cancelOrder(String orderId) {
        if (ObjectUtils.isEmpty(orderId)) {
            return this.error("订单Id" + ErrorMsg.MUST_NOT_NULL);
        }
        return orderService.cancelOrder(orderId);
    }

    @ApiOperation("处理取消订单")
    @PostMapping("/handlerCancelOrder")
    public BaseResponse handlerCancelOrder(@RequestBody OrderVo orderVo) {
        if (StringUtils.isAnyBlank(orderVo.getCode(), orderVo.getId())) {
            return this.error("订单Id" + ErrorMsg.MUST_NOT_NULL);
        }
        return orderService.handlerCancelOrder(orderVo);
    }

    @Override
    @ApiOperation("修改订单信息")
    public BaseResponse saveOrUpdate(@NotNull @RequestBody OrderVo vo) {
        if (StrUtil.isBlank(vo.getId())) {
            return this.error("订单Id不能为空！！！");
        }
        return this.orderService.updateOrderInfo(vo);
    }

    @ApiOperation("获取当前订单日志")
    @PostMapping("/getCurrentOrderLog")
    public BaseResponse order(@NotNull String orderCode) {
        log.info("调用日志服务获取当前订单日志");
        BaseResponse<List<OrderLogInfo>> response = logFeignServer.getCurrentOrderLog(orderCode);
        if (!response.isSuccess()) {
            log.info(response.getMessage());
            return this.error(response.getMessage());
        }
        List<OrderLogInfo> orderLogInfoList = response.getData();
        log.info("当前订单日志:{}", JSON.toJSONString(orderLogInfoList));
        return this.success(orderLogInfoList);
    }

    @ApiOperation("获取当前用户订单信息")
    @PostMapping("/getUserOrder")
    public BaseResponse getUserOrder(@RequestBody PageVo pageVo) {
        return super.pageConditionsList(pageVo);
    }
}
